package cardGame;

import java.util.List;
import java.util.Scanner;

public class WarGame extends Game {

	@Override
	protected int getMaxPlayers() {
		return 25;
	}

	@Override
	protected int getMinPlayers() {
		return 2;
	}

	protected void play(int numPlayers) {
		if (numPlayers <= getMaxPlayers() && numPlayers >= getMinPlayers()){
		initPlayers(numPlayers);
		deal(new HoyleDeck(), numPlayers);
		score();
		}
		else {
			System.out.println("Oops. Not the right number of players.");
		}
	}

	private void initPlayers(int numPlayers) {
		for (int i = 0; i < numPlayers; i++) {
			this.players.add(new Player("Player" + (i + 1)));
		}
	}

	private void deal(HoyleDeck deck, int numPlayers) {
		deck.shuffle();
		int numDealt = 0;
		Card card = deck.deal(numPlayers);
		while (card != null) {
			this.players.get(numDealt++ % numPlayers).getHand().add(card);
			card = deck.deal(numPlayers);
		}
	}

	private void score() {
		for (Player p : this.players) {
			int playerScore = countHand(p.getHand());
			System.out.println(p.getName() + ": " + playerScore);
		}
	}

	private int countHand(List<Card> hand) {
		int sum = 0;
		for (Card card : hand) {
			// if (card.getFaceValue().ordinal() + 1 <= 10){
			sum += card.getFaceValue().ordinal() + 1;
			// }
			// else {
			// sum += 10;
			// }
		}
		return sum;
	}
	

	public static void main(String[] args) {
		System.out.println("Enter the number of players (2 to 25): ");
		Scanner sc = new Scanner(System.in);
		int numPlayers = sc.nextInt();
		new WarGame().play(numPlayers);
	}


}
